<?php

namespace App\Transformers;

use App\Models\Article;
use League\Fractal\TransformerAbstract;

class ArticleTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['node', 'tags', 'user', 'content'];

    public function transform(Article $article)
    {
        return $article->attributesToArray();
    }

    public function includeNode(Article $article)
    {
        if (!$article->node) {
            return $this->null();
        }

        return $this->item($article->node, new NodeTransformer());
    }

    public function includeTags(Article $article)
    {
        if (!$article->tags) {
            return $this->null();
        }

        return $this->collection($article->tags, new TagTransformer());
    }

    public function includeUser(Article $article)
    {
        if (!$article->user) {
            return $this->null();
        }

        return $this->item($article->user, new UserTransformer());
    }

    public function includeFeed(Article $article)
    {
        if (!$article->content) {
            return $this->null();
        }

        return $this->item($article->content, new ContentTransformer());
    }
}